//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
const Debug = require('../../utils/Debug.js')
Page({
  data: {
    pages: 1, //默认第一页
    pageSize: 10, //每页10个
    title: "名家/剧团", //当前页面的标题
    celebrityclass: ["名家", "剧团"],
    navactive: 0, //默认名家
    channel: "", //渠道
          jiazai: false, //加载
    list: [], //名家
          listbox:[],//剧团
    mingjia: ["喜欢听秦腔？你找了很久的秦腔名家/剧团都在这里", "名人唱腔,这里你一定不能错过"],
    share_user: "", //分享用户
    swiperHide: 0
  },
  onLoad: function(options) {
    var that = this
          if (options.navactive!=undefined){
                  that.setData({
                          navactive: options.navactive
                  })
          }
    that.setData({
      headerheight: app.globalData.navHeight,

    })
    that.request()
    // 判断入口
    if (!(options.user_id == undefined)) {
      that.setData({
        share_user: options.user_id
      })
    }
  },
  bindChange: function(e){
    console.log('滑动')
          wx.pageScrollTo({
                  scrollTop: 0,
                  duration: 0
          })
    this.setData({
      navactive: e.detail.current
    })
         if (this.data.list.length == 0){
                 this.request()
        } else if (this.data.listbox.length == 0) {
                this.request()
        }
          if (this.data.navactive == 0) {
                  if (this.data.list.length > 0) {
                          this.setData({
                                  swiperHide: this.data.list.length / 2 * 333
                          })

                  }
          } else if (this.data.navactive == 1) {
                  if (this.data.listbox.length > 0) {
                          this.setData({
                                  swiperHide: this.data.listbox.length * 300 + 200
                          })

                  }
          }
  },
  // 添加客服
  kefu: function() {
    this.setData({
      kefushow: true
    })
  },
//   重新加载
jiazai: function (e) {
        this.setData({
                navactive: e.currentTarget.dataset.index,
                list: []
        })
        this.request()
},
  // 名家剧团
  navactive: function(e) {
    console.log('点击')
          wx.pageScrollTo({
                  scrollTop: 0,
                  duration: 0
          })
    this.setData({
      navactive: e.currentTarget.dataset.index
    })
  },
  // 登录
  login: function() {
    var that = this
    wx.login({
      success: function(res) {
        var code = res.code
        var data = {
          userCode: code,
          channel: that.data.channel,
          userImageUrl: "",
          userName: "",
          promoterId: that.data.share_user
        }
        util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
      }
    })
  },
  succeed: function(res) {
    Debug.debug("<------- 登录------>")
    Debug.debug(res)
    wx.setStorageSync('tekon', res.data.token)
    wx.setStorageSync('userInfo', res.data.user)
    this.request()
  },


  onShareAppMessage: function(res) {
    var that = this
    return {
      title: that.data.mingjia[Math.floor(Math.random() * that.data.mingjia.length)],
      imageUrl: "https://img-1256481299.cos.ap-guangzhou.myqcloud.com/qqhxcx/mj.jpg",
      path: 'pages/meet/meet?user_id=' + wx.getStorageSync("userInfo").id,
    }
  },

  // 列表请求
  request: function() {
    var that = this
    wx.showLoading({
      title: '努力加载中...',
    })
    var data = {
      pageIndex: that.data.pages,
      pageSize: that.data.pageSize
    };
    var header = {
      'authorization': wx.getStorageSync('tekon'),
      'content-type': "application/x-www-form-urlencoded"
    }
    if (that.data.navactive == 0) {
      //名家列表
      util.formhttp(app.globalData.appurl + "/qinqiang/famous?categoryId=4", 'GET', data, header, that.porcessData);
    } else if (that.data.navactive == 1) {
      //剧团列表
      util.formhttp(app.globalData.appurl + "/qinqiang/troupe?categoryId=4", 'GET', data, header, that.porcessDatas);
    }

  },
  //列表成功的处理
  porcessData: function(res) {
    Debug.debug("<------- 名家/剧团------->")
    Debug.debug(res)
          if (res.code == 1) {
                  this.setData({
                          jiazai: true
                  })
          } else {
                  this.setData({
                          jiazai: false
                  })
          }
    if (res.status == 401) {
      this.login()
    }
    wx.hideLoading()
    var list = [];
    var totalList = [];
    for (var ids in res.data) {
      var detail = res.data[ids];
      var temp = {
        id: detail.id,
        name: detail.name.replace(/\s+/g, ""),
        imgUrl: detail.imgUrl,
        videoCount: detail.videoCount
      }
      list.push(temp)
    }
    totalList = this.data.list.concat(list);
    this.setData({
      list: totalList,
      swiperHide: totalList.length/2*333
    })
  },
  porcessDatas: function(res) {
    if (res.status == 401) {
      this.login()
    }
    wx.hideLoading()
    console.log(res.data)
    this.setData({
      listbox: res.data,
      swiperHide: res.data.length*300+200
    })
  }
})